<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>手机号:<input type="text" class="phone" maxlength="11" min="0" onkeyup="value=value.replace(/[^0-9]/g,'')"/>&nbsp&nbsp&nbsp<input type="button" value="获取验证码" class="btn"></td>
    </tr>
    <tr>
        <td>验证码:<input type="text" class="msg"><span class="spa"></span></td>
    </tr>
    <tr>
        <td>密码:<input type="password" class="loginpwd"></td>
    </tr>
    <tr>
        <td>用户名:<input type="username" class="username"></td>
    </tr>
    <tr>
        <td>邮箱:<input type="text" class="email"></td>
    </tr>
    <tr aria-rowspan="1">
        <td><input type="button" value="点击注册" class="regist"></td>
    </tr>
</table>
</body>
</html>
<script src="jquery.js"></script>
<script>
    $(function(){
        $(".btn").css("background","#E1E1E1");
        $(".btn").css("color","#808080");
            $(".phone").blur(function () {
                var phone = $(".phone").val();
                if(phone.length==11){
                    $(".btn").css("background","#ffffff");
                    $(".btn").css("color","black");
                }
                if(phone.length<11){
                    $(".btn").css("background","#E1E1E1");
                    $(".btn").css("color","#808080");
                }
            })
            $(".btn").live("click",function () {
                var phone = $(".phone").val();
                if(phone.length<11){
                    return alert("手机号必须是11位");
                }
                var num=60;
                var time=setInterval(function(){
                    $(".btn").val("获取验证码("+num+")");
                    $(".btn").attr("disabled","true");
                    $(".btn").attr("disabled",true);
                    $(".btn").attr("disabled","disabled");
                    num--;
                    if(num<0){
                        clearInterval(time);
                        $(".btn").val("获取验证码");
                        $(".btn").removeAttr("disabled");
                        $(".btn").attr("disabled",false);
                    }
                },1000)
                $.ajax({
                    url:"random",
                    type:"post",
                    data:{"phone":phone},
                    success:function (data) {
                        if(data=="服务器内部错误"){
                            alert(data);
                        }
                    }
                })
            })
        $(".msg").blur(function () {
            var msg = $(".msg").val();
            $.ajax({
                url:"chemsg",
                type:"POST",
                data:{"msg":msg},
                success:function(data){
                    data = data.replace("\"","").replace("\"","");
                    if(msg.length==0){
                        return "";
                    }else{
                        if(data=="验证码错误"){
                            $(".spa").html(data);
                            $(".regist").attr("disabled","true");
                            $(".regist").attr("disabled",true);
                            $(".regist").attr("disabled","disabled");
                        }else{
                            $(".spa").html(data);
                            $(".regist").removeAttr("disabled");
                            $(".regist").attr("disabled",false);
                        }
                    }
                }
            })
        })
        $(".msg").focus(function () {
            $(".spa").html("");
        })
        $(".regist").click(function () {
            var phone = $(".phone").val();
            var loginpwd = $(".loginpwd").val();
            var username = $(".username").val();
            var email = $(".email").val();
            var msg=$(".msg").val();
            if(loginpwd==""||phone==null){
                alert("密码不能为空");
                return false;
            }
            if(username==""||phone==null){
                alert("用户名不能为空");
                return false;
            }
            if(email==""||phone==null){
                alert("邮箱不能为空");
                return  false;
            }
            if(phone==""||phone==null){
                alert("手机号不能为空");
                return false;
            }
            if(msg==""||msg==null){
                alert("请填写验证码");
                return  false;
            }
           if(msg!=null||msg!=""){
               $.ajax({
                   url:"chemsg",
                   type:"POST",
                   data:{"msg":msg},
                   success:function(data){
                       data = data.replace("\"","").replace("\"","");
                       if(data=="验证码错误"){
                           $(".spa").html(data);
                           return false;
                       }else{
                           $.ajax({
                               url:"add",
                               type:"POST",
                               data:{"phone":phone,"loginpwd":loginpwd,"username":username,"email":email},
                               dataType:"json",
                               success:function(data){
                                   if(data=="1"){
                                       alert("注册成功!");
                                       location.reload();
                                   }else{
                                       alert("注册失败");
                                       location.reload();
                                   }
                               },error:function () {
                                   alert("错误")
                               }
                           })
                       }
                       // if(msg.length==0){
                       //     return "";
                       // }else{
                       //
                       //     return false;
                       // }
                   }
               })
           }
           //  else{
           //      $.ajax({
           //          url:"chemsg",
           //          type:"POST",
           //          data:{"msg":msg},
           //          success:function(data){
           //              data = data.replace("\"","").replace("\"","");
           //              if(msg.length==0){
           //                  return "";
           //              }else{
           //                  if(data=="验证码错误"){
           //                      $(".spa").html(data);
           //                      return false;
           //                  }
           //                  return false;
           //              }
           //          }
           //      })
           // }
        })
        })
</script>